import pickle
import json

d = dict(name='Bob', age=20, score=88)
# pickle.dumps(d)
f = open('dump.txt', 'wb')
pickle.dump(d, f)
f.close()

rf = open('dump.txt', 'rb')
rd = pickle.load(rf)
rf.close()
print(rd)

#JSON化
print(json.dumps(d))

str='{"name": "Bob", "age": 20, "score": 88}'

print(json.loads(str))

#class JSON化

def dict2student(d):
    return Student(d['name'], d['age'], d['score'])

class Student(object):
    def __init__(self, name, age, score):
        self.name = name
        self.age = age
        self.score = score

s = Student('Bob', 20, 88)

print(json.dumps(s, default=lambda obj: obj.__dict__))

print(json.loads(str, object_hook=dict2student))

